'''
不用辅助空间，可以设置快慢指针判断是否有环

'''


# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    def hasCycle(self, head) :
        if not head:
            return False

        if head.next == None :
            return False
        i = head
        j = head
        while True:
            if i==None or j ==None:
                return False
            i = i.next
            j = j.next
            if i==None or j ==None:
                return False
            j = j.next
            if i==j:
                return True


if __name__ == '__main__':
    # z = ListNode()
    one = ListNode(3)
    two = ListNode(2)
    three = ListNode(0)
    four = ListNode(-4)
    one.next = two
    two.next = three
    three.next = four
    four.next = two
    S = Solution()
    T = S.hasCycle(one)
    print(T)
